<?php

namespace App\Http\Controllers\Api;

use App\ApiResponse;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Http\Resources\InCodeResource;
use App\Models\InCode;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class InCodeController extends Controller
{
    use ApiResponse;

    /**
     * Index interface.
     *
     *
     */
    public function index(Request $request)
    {
        $user_id = $request->user('api')->id;

        $codes = InCode::whereUserId($user_id)->orderBy('status','asc')->get();
//        dd($codes);
        return $this->message('获取成功', InCodeResource::collection($codes));

    }

    public function load(Request $request){
        $user = $request->user('api');

        return $this->message('获取成功',['in_code'=> $user->phone]);
    }

//    /**
//     * Edit interface.
//     *
//     * @param $id
//     * @return Content
//     */
//    public function edit($id)
//    {
//        return Admin::content(function (Content $content) use ($id) {
//
//            $content->header('邀请码');
//            $content->description('编辑');
//
//            $content->body($this->form()->edit($id));
//        });
//    }


    public function store(Request $request)
    {
        $number = $request->input("number", 0);
        if ($number >= 0 && $number <= 100) {
            $user_id = $request->user('api')->id;

            for ($i = 0; $i < $number; $i++) {
                $random = md5(random_int(1000, 4000) . time() . $user_id);
                $insert['code'] = $random;
                $insert['user_id'] = $user_id;
                $insert['status'] = 0;
                InCode::query()->create($insert);
            }
            return $this->message('生成成功');
//            return false;
        } else {
            return $this->message('生成个数必须大于0',[],400);
        }
    }
}
